layui.define(['jquery', 'layer'], function(exports) {
	var $ = layui.jquery,
		layer = layui.layer;
	$.PojectId = "12345";
	$.Common = {
		Config: {
			server: "http://localhost:8989" // 结尾不带/
		},
		Http: {
			Get: function(url, success, error) {
				var index = $.Common.Dialog.loading();
				url = url.replace("baseUrl", $.Common.Config.server);
				$.ajax({
					url: url,
					type: "get",
					success: function(data) {
						$.Common.Dialog.close(index);
						if(data.code == 200) {
							if(data.result != null && success) {
								success(data.result)
							} else {
								success()
							}
						} else {
							$.Common.Dialog.showTip(data.msg)
						}
					},
					error: function(xhr, e, code) {
						$.Common.Dialog.close(index);
						$.Common.Dialog.showTip("网络请求错误！")
						if(error) {
							error(xhr, error, code)
						}
					}
				});
			},
			Post: function(url, params, success, error) {
				var index = $.Common.Dialog.loading();
				url = url.replace("baseUrl", $.Common.Config.server);
				$.ajax({
					url: url,
					data: {
						"data": JSON.stringify(params)
					},
					type: "post",
					success: function(data) {
						$.Common.Dialog.close(index);
						if(data.code == 200) {
							if(data.result != null && success) {
								success(data.result)
							} else {
								success()
							}
						} else {
							$.Common.Dialog.showTip(data.msg)
						}
					},
					error: function(xhr, e, code) {
						$.Common.Dialog.close(index);
						$.Common.Dialog.showTip("网络请求错误！")
						if(error) {
							error(xhr, error, code)
						}
					}
				});
			}
		},
		Dialog: {
			info: function(text) {

				layui.layer.alert(text, {
					btnAlign: 'c'
				});

			},
			showTip: function(text) {

				layui.layer.msg(text);

			},
			confirm: function(text, ok, cancle) {

				layui.layer.confirm('<div>' + text + '</div>', {
					btn: ['确定', '取消'],
					btnAlign: 'c',
					btn3: function(index, layero) {
						layer.closeAll();
						if(cancle) {
							cancle();
						}
					}
				}, function(index, layero) {
					if(ok) {
						ok();
					}
				});

			},
			loading: function() {
				return layui.layer.load();
			},
			close: function(index) {
				layui.layer.close(index);
			},
			closeAll: function() {
				layui.layer.closeAll();
			}
		},
		Utils: {

			GetQueryString: function(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if(r != null) return unescape(r[2]);
				return null;
			}
		}
	};

	// 编译相关的请求
	$.Build = {
		build: function() {
			$.Common.Http.Get("baseUrl/cmd/build", function(data) {
				$.Common.Dialog.showTip("编译成功!");
			});
		},
		rebuild: function() {
			$.Common.Http.Get("baseUrl/cmd/rebuild", function(data) {
				$.Common.Dialog.showTip("编译成功!");
			});
		},
		clean: function() {
			$.Common.Http.Get("baseUrl/cmd/clean", function(data) {
				$.Common.Dialog.showTip("清理成功!");
			});
		}
	};

	// 调试运行相关的请求
	$.Run = {
		run: function() {
			$.Common.Http.Get("baseUrl/cmd/run", function(data) {
				window.timer = setInterval(function() {
					$.ajax({
						type: "get",
						dataType: 'jsonp',
						url: "http://localhost:8080/ims",
						async: true,
						success: function(data) {
							window.open("http://localhost:8080/ims", "_blank");
							clearInterval(window.timer);
						},
						error: function(x, error, c) {
							console.log("请求中...");
							if(error != "error" && !window.isopen) {
								window.isopen = true;
								window.open("http://localhost:8080/ims", "_blank");
								clearInterval(window.timer);
							}
						}
					});
				}, 5000);
			}, function() {

			});
		},
		stop: function() {
			$.Common.Http.Get("baseUrl/cmd/run", function(data) {
				window.isopen = false;
				$.Common.Dialog.showTip("停止成功");
			});
		}
	}

	// 项目管理相关的请求
	$.Project = {
		File: function(id, name, group, category) {
			return {
				ID: id,
				Name: name,
				Group: group,
				Category: category,
				Type:""
			};
		},
		loadProject: function(success) {
			$.Common.Http.Post("baseUrl/project/load", {
				project: $.PojectId
			}, function(data) {
				success(data);
			});
		},
		addFile: function(file, success, error) {
			file.Project = $.PojectId;
			$.Common.Http.Post("baseUrl/project/add",file,
			function(data) {
				success(data);
			});
		},
		addGroup: function() {

		},
		delFile: function(file, success, error) {
			file.Project = $.PojectId;
			$.Common.Http.Post("baseUrl/project/del",file,
			function(data) {
				success(data);
			});
		},
		delGroup: function() {

		},
		renameFile: function(file, success, error) {
			file.Project = $.PojectId;
			$.Common.Http.Post("baseUrl/project/rename",file,
			function(data) {
				success(data);
			});
		},
		regroup: function() {
			file.Project = $.PojectId;
			$.Common.Http.Post("baseUrl/project/regroup",file,
			function(data) {
				success(data);
			});
		}

	};

	// 数据对象编辑相关的请求
	$.DBEditor = {
		Field: function(code, name, type, length, comment) {
			return {
				Code: code,
				Name: name,
				Type: type,
				Length: length,
				Comment: comment
			}
		},
		loadField: function(id, success) {
			$.Common.Http.Post("baseUrl/editor/db/load", {
				id: id
			}, function(data) {
				success(data);
			});
		},
		addField: function(id, field, success) {
			$.Common.Http.Post("baseUrl/editor/db/add_field", {
				id: id,
				field: field
			}, function(data) {
				success(data);
			});
		},
		updField: function(id, field, success) {
			$.Common.Http.Post("baseUrl/editor/db/upd_field", {
				id: id,
				field: field
			}, function(data) {
				success(data);
			});
		},
		delField: function(id, field, success) {
			$.Common.Http.Post("baseUrl/editor/db/del_field", {
				id: id,
				field: field
			}, function(data) {
				success(data);
			});
		}
	}

	// 业务流程编辑相关的请求
	$.LogicEditor = {
		Func: function(id, name, Desc) {
			return {
				Id: id,
				Name: name,
				Desc: Desc
			}
		},
		loadFuncs:function(fileId,success){
			$.Common.Http.Get("../../data/test_funcs.json", function(data) {
				if(data) {
					success(data);
				}
			});
		},
		loadFuncDef:function(fileId,funcId,success){
			$.Common.Http.Get("../../data/test_func.json?id=" + funcId, function(data) {
				if(data) {
					success(data);
				}
			});
		},
		addFunc:function(fileId,func,success){
			
			
		},
		
		updFunc:function(fileId,func,success){
			
			
		},
		delFunc:function(fileId,func,success){
			
			
		}
	}

	// UI编辑相关的请求
	$.UIEditor = {

	}
	exports('common', $);
});